﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_HappyPlanSrhForm
 * 类型：       控制层
 * 功能说明：   快乐管家-活动计划检索页面
 * 作者：       章 昕
 * 创建日期：   2007/11/05
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using log4net;
using Shpm.App.Commons.Utils;
using Shpm.System.Validator;
using Shpm.System.Web.Control;

public partial class App_Input_HappyPlanSrhForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_HappyPlanSrhForm));

    // 检索按钮 click 标志    
    private const string COMMAND_SEARCH = "DoSearch";

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            initPageControls();

            // 接收传递参数并初始化控件
            txtActName.Text = Convert.ToString(Context.Items["InpActName"]);
            lstActType.SelectedValue = Convert.ToString(Context.Items["InpActType"]);
            lstActStat1.SelectedValue = Convert.ToString(Context.Items["InpActStat1"]);
            lstActStat2.SelectedValue = Convert.ToString(Context.Items["InpActStat2"]);
            txtBeginDate.Text = Convert.ToString(Context.Items["InpBeginDate"]);
            txtEndDate.Text = Convert.ToString(Context.Items["InpEndDate"]);

            // 接收传递参数为检索条件
            ViewState["HidActName"] = Convert.ToString(Context.Items["HidActName"]);
            ViewState["HidActType"] = Convert.ToString(Context.Items["HidActType"]);
            ViewState["HidActStat1"] = Convert.ToString(Context.Items["HidActStat1"]);
            ViewState["HidActStat2"] = Convert.ToString(Context.Items["HidActStat2"]);
            ViewState["HidBeginDate"] = Convert.ToString(Context.Items["HidBeginDate"]);
            ViewState["HidEndDate"] = Convert.ToString(Context.Items["HidEndDate"]);
            ViewState["HidCommand"] = Convert.ToString(Context.Items["HidCommand"]);

            // 接收传递参数为分页编号
            if (Context.Items.Contains("HidPageIndex"))
            {
                viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["HidPageIndex"]);
            }
        }

        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
        PersonEmployeeBean beanEmp = new PersonEmployeeBean();
        HappyPlanDao dao = new HappyPlanDao();
        beanEmp.EmployeeId = emplyId;
        beanEmp = dao.GetEmployeeJob(beanEmp);
        if (beanEmp == null)
        { ViewState["EMP_LIMIT"] = "0"; }
        else
        { ViewState["EMP_LIMIT"] = (beanEmp.JobLimit == null) ? "0" : Convert.ToString(beanEmp.JobLimit); }
    }

    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        tableResult.Visible = false;
        // reload list
        if (COMMAND_SEARCH.Equals(ViewState["HidCommand"].ToString()))
        {
            HappyPlanDao dao = new HappyPlanDao();
            HappyPlanBean beanHP = new HappyPlanBean();
            IList lstRst = null;

            beanHP.ActionName = Convert.ToString(ViewState["HidActName"]);
            beanHP.ActionType = Convert.ToString(ViewState["HidActType"]);
            beanHP.DepartmentAdvice = Convert.ToString(ViewState["HidActStat1"]);
            beanHP.ManagerAdvice = Convert.ToString(ViewState["HidActStat2"]);
            if (!string.IsNullOrEmpty(Convert.ToString(ViewState["HidBeginDate"])))
            { beanHP.BeginDate = DateUtils.convertToDate(Convert.ToString(ViewState["HidBeginDate"])); }
            if (!string.IsNullOrEmpty(Convert.ToString(ViewState["HidEndDate"])))
            { beanHP.EndDate = DateUtils.convertToDate(Convert.ToString(ViewState["HidEndDate"])); }

            lstRst = dao.ListHappyPlan(beanHP);
            tableResult.Visible = true;
            if (lstRst == null || lstRst.Count == 0)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0005").ToString();
                tableResult.Visible = false;
            }
            viewSrhRst.DataSource = lstRst;
            viewSrhRst.DataBind();
        }
    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // link to popup page 
            ((LinkButton)e.Row.FindControl("lkbActName")).Text = ((HappyPlanBean)e.Row.DataItem).ActionName;
            string actionId = Convert.ToString(((HappyPlanBean)e.Row.DataItem).ActionId);
            ((LinkButton)e.Row.FindControl("lkbActName")).Attributes.Add("onclick", "openActionDetail('" + actionId + "');return false;");

            // column 8 是否提交
            ((ImageButton)e.Row.FindControl("ImageButton2")).Enabled = false;
            e.Row.Cells[7].Text = "编辑中";
            if (((HappyPlanBean)e.Row.DataItem).CommitFlg != null 
                && ((HappyPlanBean)e.Row.DataItem).CommitFlg.Value)
            {
                e.Row.Cells[7].Text = "已提交";
                ((ImageButton)e.Row.FindControl("ImageButton2")).Enabled = true;
            }
            
            // 是否已经审批 
            ((ImageButton)e.Row.FindControl("ImageButton3")).Enabled = false;
            ((ImageButton)e.Row.FindControl("ImageButton4")).Enabled = false;
            ((ImageButton)e.Row.FindControl("ImageButton5")).Enabled = false;
            if (((HappyPlanBean)e.Row.DataItem).AuditingFlg != null 
                && ((HappyPlanBean)e.Row.DataItem).AuditingFlg.Value
                //&& "1".Equals(((HappyPlanBean)e.Row.DataItem).DepartmentAdvice)
                && "1".Equals(((HappyPlanBean)e.Row.DataItem).ManagerAdvice))
            {
                e.Row.Cells[7].Text = "已审核";
                ((ImageButton)e.Row.FindControl("ImageButton3")).Enabled = true;
                ((ImageButton)e.Row.FindControl("ImageButton4")).Enabled = true;
                ((ImageButton)e.Row.FindControl("ImageButton5")).Enabled = true;
            }

            // 是否有权限审批
            if (Convert.ToInt32(ViewState["EMP_LIMIT"]) == 0)
            {
                // 快乐管家
                ((ImageButton)e.Row.FindControl("ImageButton2")).Enabled = false;
                ((ImageButton)e.Row.FindControl("ImageButton2")).Enabled = false;
                ((ImageButton)e.Row.FindControl("ImageButton2")).Enabled = false;
            }
            else 
            {
                // 审批领导
                ((ImageButton)e.Row.FindControl("ImageButton1")).Enabled = false;
                ((ImageButton)e.Row.FindControl("ImageButton1")).Enabled = false;
                ((ImageButton)e.Row.FindControl("ImageButton1")).Enabled = false;
            }
        }
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    // 检索按钮
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ComValidateChecker vd = new ComValidateChecker();
        vd.Add(new CheckDate(txtBeginDate.Text, new ComMessage("E0037", "开始日期")));
        vd.Add(new CheckDate(txtEndDate.Text, new ComMessage("E0037", "结束日期")));
        vd.Add(new CheckDateRange(txtBeginDate.Text, txtEndDate.Text, new ComMessage("E0329", new string[] { "开始日期", "结束日期" })));
        if (!vd.DoValidator())
        {
            ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
            return;
        }

        ViewState["HidActName"] = txtActName.Text;
        ViewState["HidActType"] = "";
        if (lstActType.Items.Count > 0 && lstActType.SelectedIndex > 0)
        { ViewState["HidActType"] = lstActType.SelectedValue; }
        ViewState["HidActStat1"] = "";
        if (lstActStat1.Items.Count > 0 && lstActStat1.SelectedIndex > 0)
        { ViewState["HidActStat1"] = lstActStat1.SelectedValue; }
        ViewState["HidActStat2"] = "";
        if (lstActStat2.Items.Count > 0 && lstActStat2.SelectedIndex > 0)
        { ViewState["HidActStat2"] = lstActStat2.SelectedValue; }
        ViewState["HidBeginDate"] = txtBeginDate.Text;
        ViewState["HidEndDate"] = txtEndDate.Text;
        ViewState["HidCommand"] = COMMAND_SEARCH;
        viewSrhRst.PageIndex = 0;
    }

    // 新建按钮
    protected void btnNew_Click(object sender, EventArgs e)
    {
        // Transfer Page        
        Context.Items["ACTION_ID"] = "";
        Context.Items["MODE"] = "EDIT";
        gotoPage("~/App/Input/HappyPlanUpdForm.aspx");
    }

    // 操作 button click
    protected void ImageButton_Command(object sender, CommandEventArgs e)
    {
        string directUrl;
        switch (e.CommandName)
        {
            case "DETAIL":   // 编辑
                directUrl = "~/App/Input/HappyPlanUpdForm.aspx";
                Context.Items["MODE"] = "EDIT";
                break;
            case "AUDITING": // 审核
                directUrl = "~/App/Input/HappyPlanUpdForm.aspx";
                Context.Items["MODE"] = "AUDI";
                break;
            case "INTENT":   // 报名
                directUrl = "~/App/Input/HappyIntentUpdForm.aspx";                
                break;
            case "EVALUATE": // 评估
                directUrl = "~/App/Input/HappyEvaluateUpdForm.aspx";                
                break;
            case "STAT.":    // 统计
                directUrl = "~/App/Input/HappyEvaluateStatForm.aspx";                
                break;                   
            default:
                directUrl = "~/App/Input/HappyPlanSrhForm.aspx";
                break;
        }

        // Transfer Page        
        Context.Items["ACTION_ID"] = Convert.ToString(e.CommandArgument);
        gotoPage(directUrl);
    }

    // 参数传递 and 页面迁移
    private void gotoPage(string url)
    {
        // 传递控件输入内容
        Context.Items["InpActName"] = txtActName.Text;
        Context.Items["InpActType"] = lstActType.SelectedValue;
        Context.Items["InpActStat1"] = lstActStat1.SelectedValue;
        Context.Items["InpActStat2"] = lstActStat2.SelectedValue;
        Context.Items["InpBeginDate"] = txtBeginDate.Text;
        Context.Items["InpEndDate"] = txtEndDate.Text;

        // 传递检索条件
        Context.Items["HidActName"] = Convert.ToString(ViewState["HidActName"]);
        Context.Items["HidActType"] = Convert.ToString(ViewState["HidActType"]);
        Context.Items["HidActStat1"] = Convert.ToString(ViewState["HidActStat1"]);
        Context.Items["HidActStat2"] = Convert.ToString(ViewState["HidActStat2"]);
        Context.Items["HidBeginDate"] = Convert.ToString(ViewState["HidBeginDate"]);
        Context.Items["HidEndDate"] = Convert.ToString(ViewState["HidEndDate"]);
        Context.Items["HidCommand"] = Convert.ToString(ViewState["HidCommand"]);

        // 传递分页编号
        Context.Items["HidPageIndex"] = viewSrhRst.PageIndex;
        ControllerUtils.Transfer(Context, url, true);
    }

    // bind common catalog data 
    private void initPageControls()
    {
        HappyPlanDao dao = new HappyPlanDao();
        CommonDictionaryBean beanComDict = new CommonDictionaryBean();
        IList lstCat = null;

        // 活动分类
        log.Fatal("\n ***** get 活动分类 catalog *****");
        beanComDict.Catalog = "T_ACTION_ADVICE.ADVICE_TYPE";
        lstCat = dao.FindByCatalog(beanComDict);
        lstActType.Items.Clear();
        lstActType.Items.Add("");
        bindDataToListControl("lstActType", lstCat);

        // 审核状态
        log.Fatal("\n ***** get 审核状态 catalog *****");
        beanComDict.Catalog = "T_ACTION_ADVICE.DEPARTMENT_ADVICE";
        lstCat = dao.FindByCatalog(beanComDict);
        lstActStat1.Items.Clear();
        lstActStat2.Items.Clear();
        lstActStat1.Items.Add("");
        //lstActStat1.Items.Add(new ListItem("等待","0"));
        lstActStat2.Items.Add("");
        //lstActStat2.Items.Add(new ListItem("等待", "0"));
        bindDataToListControl("lstActStat1", lstCat);
        bindDataToListControl("lstActStat2", lstCat);
    }

    // bind db data to selected controls
    private void bindDataToListControl(string ControlId, IList LstData)
    {
        if (LstData == null || LstData.Count == 0) return;

        foreach (CommonDictionaryBean bean in LstData)
        {
            if (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is DropDownList)
            {
                ((DropDownList)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
            else if ((this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is CheckBoxList)
                || (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is RadioButtonList))
            {
                ((ListControl)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
        }
    }

}
